import AppKit

// 定义任务类型枚举
enum TaskType {
    case manualEncryption
    case autoScanning
}

class WindowDelegate: NSObject, NSWindowDelegate {
    var isProcessing: Bool
    var isChinese: Bool
    var taskType: TaskType
    var onWindowClose: (() -> Void)?
    
    init(isProcessing: Bool, isChinese: Bool, taskType: TaskType, onWindowClose: (() -> Void)? = nil) {
        self.isProcessing = isProcessing
        self.isChinese = isChinese
        self.taskType = taskType
        self.onWindowClose = onWindowClose
        super.init()
    }
    
    func windowShouldClose(_ sender: NSWindow) -> Bool {
        // 如果没有正在处理的任务，直接关闭
        if !isProcessing {
            // 调用关闭回调
            onWindowClose?()
            return true
        }
        
        // 创建确认对话框
        let alert = NSAlert()
        alert.messageText = localized("close_window_title", isChinese: isChinese)
        
        // 根据任务类型显示不同的提示信息
        switch taskType {
        case .manualEncryption:
            alert.informativeText = localized("close_window_message", isChinese: isChinese)
        case .autoScanning:
            alert.informativeText = localized("close_window_message", isChinese: isChinese)
        }
        
        alert.alertStyle = .warning
        alert.addButton(withTitle: localized("confirm", isChinese: isChinese))
        alert.addButton(withTitle: localized("cancel", isChinese: isChinese))
        
        let response = alert.runModal()
        
        // 如果用户确认关闭，调用关闭回调并返回 true；否则返回 false
        if response == .alertFirstButtonReturn {
            onWindowClose?()
            return true
        }
        return false
    }
}
